package com.itcam.tingshu.album.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcam.tingshu.model.album.TrackInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itcam.tingshu.vo.album.AlbumTrackListVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface TrackInfoMapper extends BaseMapper<TrackInfo> {


    /**
     * 修改声音排序
     * @param orderNum
     * @param albumId
     */
    @Update("update track_info set order_num = order_num - 1 " +
            "where order_num > #{orderNum}  " +
            "and album_id = #{albumId} " +
            "and is_deleted = 0")
    void updateOrderNum(@Param("orderNum") Integer orderNum, @Param("albumId") Long albumId);

    /**
     * 根据专辑Id查询声音分页列表
     * @param albumTrackListVoPage
     * @param albumId
     * @return
     */
    // @Select("select * from track_info track where album_id = #{albumId} is_deleted = 0")
    @Select("select\n" +
            "    track.id as trackId,\n" +
            "    track.track_title as trackTitle,\n" +
            "    track.media_duration as mediaDuration,\n" +
            "    track.order_num as orderNum,\n" +
            "    track.create_time as createTime,\n" +
            "    max(case when stat.stat_type='0701' then stat.stat_num else 0 end) playStatNum,\n" +
            "    max(case when stat.stat_type='0704' then stat.stat_num else 0 end) commentStatNum\n" +
            "from track_info track inner join track_stat stat on track.id = stat.track_id\n" +
            "where album_id = #{albumId}\n" +
            "and track.is_deleted = 0\n" +
            "group by stat.track_id;")
    IPage<AlbumTrackListVo> selectAlbumTrackPage(Page<AlbumTrackListVo> albumTrackListVoPage, Long albumId);

    /**
     * 修改声音统计数据
     * @param trackId
     * @param statType
     * @param count
     */
    @Update("update track_stat set stat_num = stat_num + #{count} where track_id = #{trackId} and stat_type = #{statType}")
    void trackStatUpdate(@Param("trackId") Long trackId, @Param("statType") String statType, @Param("count") Integer count);
}
